/**
 * 版权所有 2009-2012山东新北洋信息技术股份有限公司
 * 保留所有权利。
 */
package com.linyaonan.leetcode.easy._112;

/**
 * @ProjectName: leetcode
 * @Package: com.linyaonan.leetcode.easy._112
 * @ClassName: PathSum
 * @Author: linyaonan
 * @Date: 2019/11/4 16:24
 */
public class PathSum {

    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        try {
            addSumTreeNode(root, 0, sum);
        } catch (Exception e) {
            String message = e.getMessage();
            if ("is true".equals(message)) {
                return true;
            }
        }
        return false;
    }

    private void addSumTreeNode(TreeNode node, int count, int sum) throws Exception {
        if (node == null) {
            return;
        }
        count+=node.val;
        if (node.left == null && node.right == null) {
            if (count == sum) {
                throw new Exception("is true");
            }
        }
        addSumTreeNode(node.left, count, sum);
        addSumTreeNode(node.right, count, sum);
    }

}
